/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.session;

import frm.billingAndPaymentProcessing.entity.IncomingBillEntity;
import frm.billingAndPaymentProcessing.entity.VoucherEntity;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author xhx
 */
@Stateless
@LocalBean
public class VoucherSessionBean {
 @PersistenceContext
    private EntityManager em;
 
 @EJB
 IncomingBillSessionBean incomingBillSessionBean;

    public void createVoucher(VoucherEntity voucherEntity) {
        em.persist(voucherEntity);
    }

    public VoucherEntity getVoucherById(Long voucherId) {
        return em.find(VoucherEntity.class, voucherId);
    }

    public List<VoucherEntity> getAllVouchers() {
        Query query = em.createQuery("SELECT u FROM VoucherEntity u");
        return query.getResultList();
    }


    public void saveChanges(VoucherEntity voucherEntity) {
        em.merge(voucherEntity);
    }
    public void removeVoucher(Long voucherId){
        VoucherEntity voucher = em.find(VoucherEntity.class, voucherId);
        double amount = voucher.getAmountPaid();
        IncomingBillEntity incomingBill = voucher.getIncomingbill();
        incomingBill.addPayment(0-amount);
        incomingBillSessionBean.saveChanges(incomingBill);
        em.remove(voucher);
        
}
}
